package com.jinyou.chengfengshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jinyou.chengfengshop.Constant.AppUrl;
import com.jinyou.chengfengshop.OkHttpUtils.CallBackUtil;
import com.jinyou.chengfengshop.OkHttpUtils.OkhttpUtil;
import com.jinyou.chengfengshop.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;


/**
 * Created by zhuzhiqiang on 2018/9/19.
 */

public class LoginActivity extends AppCompatActivity{
    private Button next;
    private EditText edt;
    private TextView seller;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mPhone = edt.getText().toString();
                if(mPhone.length()==11){
                    sendCode(mPhone);
//                    Intent mIntent = new Intent(LoginActivity.this,IdentifyingCodeActivity.class);
//                    mIntent.putExtra("phone",mPhone);
//                    startActivity(mIntent);
                }else {
                    Toast.makeText(getApplicationContext(),"手机格式有误",Toast.LENGTH_SHORT).show();
                }
            }
        });

        edt = (EditText) findViewById(R.id.edt_phone);
        seller = (TextView) findViewById(R.id.seller);
        seller.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(getApplicationContext(),ShopRegistActivity.class);
                startActivity(mIntent);
            }
        });
    }


    //发送验证码
    private void sendCode(final String mPhone){
        HashMap<String,String> paramsMap = new HashMap<>();

        paramsMap.put("username",mPhone);
        paramsMap.put("sysCustomer","o2o-chengfeng");

        OkhttpUtil.okHttpPost(AppUrl.HOME_USER_REGISTER_GETTELCODE, paramsMap, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                Toast.makeText(LoginActivity.this,"失败",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response) {//请求成功时调用，执行在UI线程
                try {
                    JSONObject obj = new JSONObject(response);
                    int code = obj.getInt("status");
                    if(code == 1){
                        Intent mIntent = new Intent(LoginActivity.this,IdentifyingCodeActivity.class);
                        mIntent.putExtra("phone",mPhone);
                        startActivity(mIntent);
                    }else {
                        Toast.makeText(LoginActivity.this,obj.getString("error"),Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }

}
